<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
<!--      <el-form-item label="序列号" prop="serialNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.serialNo"-->
<!--          placeholder="请输入序列号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="批次号" prop="batchNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.batchNo"-->
<!--          placeholder="请输入批次号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="工单号" prop="docNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.docNo"-->
<!--          placeholder="请输入工单号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="物料号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="材料规格" prop="itemSpecification">-->
<!--        <el-input-->
<!--          v-model="queryParams.itemSpecification"-->
<!--          placeholder="请输入材料规格"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="材料描述" prop="itemDesc">-->
<!--        <el-input-->
<!--          v-model="queryParams.itemDesc"-->
<!--          placeholder="请输入材料描述"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="材料图号" prop="drawingNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.drawingNo"-->
<!--          placeholder="请输入材料图号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单位" prop="unit">-->
<!--        <el-input-->
<!--          v-model="queryParams.unit"-->
<!--          placeholder="请输入单位"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="类型" prop="materialType">
        <el-select
          v-model="queryParams.materialType"
          placeholder="请选择类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.OPTICORE_MATE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="品牌" prop="brand">-->
<!--        <el-input-->
<!--          v-model="queryParams.brand"-->
<!--          placeholder="请输入品牌"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数量" prop="qty">-->
<!--        <el-input-->
<!--          v-model="queryParams.qty"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态 0:未使用  1:已占用 2:已报废" prop="status">-->
<!--        <el-select-->
<!--          v-model="queryParams.status"-->
<!--          placeholder="请选择状态 0:未使用  1:已占用 2:已报废"-->
<!--          clearable-->
<!--          class="!w-240px"-->
<!--        >-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="创建日期" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
<!--      <el-form-item label="位置" prop="position">-->
<!--        <el-input-->
<!--          v-model="queryParams.position"-->
<!--          placeholder="请输入位置"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="仓库编号" prop="warehouseNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.warehouseNo"-->
<!--          placeholder="请输入仓库编号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['opticore:material-warehouse:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['opticore:material-warehouse:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="warning"
          plain
          @click="importDialogVisible = true"
          v-hasPermi="['opticore:material-warehouse:import']"
        >
          <Icon icon="ep:upload" class="mr-5px" />  导入
        </el-button>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column width="60px" label="序号" type="index"/>
      <el-table-column label="序列号" align="center" prop="serialNo" />
      <el-table-column label="物料号" align="center" prop="itemCode" />
      <el-table-column label="物料名称" align="center" prop="itemName" />
      <el-table-column label="物料类型" align="center" prop="materialType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OPTICORE_MATE_TYPE" :value="scope.row.materialType" />
        </template>
      </el-table-column>      <el-table-column label="物料单位" align="center" prop="unit" />
      <el-table-column label="物料规格" align="center" prop="itemSpecification" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="位置" align="center" prop="position" />
      <el-table-column label="数量" align="center" prop="qty" />
<!--      <el-table-column label="已占用数量" align="center" prop="allreadyQty" />-->
      <el-table-column label="物料图号" align="center" prop="drawingNo" />



<!--      <el-table-column label="批次号" align="center" prop="batchNo" />-->
<!--      <el-table-column label="工单号" align="center" prop="docNo" />-->
<!--      <el-table-column label="材料描述" align="center" prop="itemDesc" />-->
<!--      <el-table-column label="材料图号" align="center" prop="drawingNo" />-->
<!--      <el-table-column label="主图" align="center" prop="mainImage" />-->
<!--      <el-table-column label="状态 0:未使用  1:已占用 2:已报废" align="center" prop="status" />-->
<!--      <el-table-column-->
<!--        label="创建日期"-->
<!--        align="center"-->
<!--        prop="createTime"-->
<!--        :formatter="dateFormatter"-->
<!--        width="180px"-->
<!--      />-->
      <el-table-column label="仓库编号" align="center" prop="warehouseNo" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['opticore:material-warehouse:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['opticore:material-warehouse:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MaterialWarehouseForm ref="formRef" @success="getList" />

  <!-- 导入弹窗 -->
  <MaterialWarehouseImportForm
    v-model="importDialogVisible"
    @success="handleImportSuccess"
  />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { MaterialWarehouseApi, MaterialWarehouseVO } from '@/api/opticore/materialwarehouse'
import MaterialWarehouseForm from './MaterialWarehouseForm.vue'
import MaterialWarehouseImportForm from './MaterialWarehouseImportForm.vue'
import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";

/** 物料仓库 列表 */
defineOptions({ name: 'MaterialWarehouse' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<MaterialWarehouseVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialNo: undefined,
  batchNo: undefined,
  docNo: undefined,
  itemCode: undefined,
  itemName: undefined,
  itemSpecification: undefined,
  itemDesc: undefined,
  drawingNo: undefined,
  unit: undefined,
  materialType: undefined,
  brand: undefined,
  qty: undefined,
  mainImage: undefined,
  status: undefined,
  createTime: [],
  position: undefined,
  warehouseNo: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const importDialogVisible = ref(false)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MaterialWarehouseApi.getMaterialWarehousePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MaterialWarehouseApi.deleteMaterialWarehouse(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MaterialWarehouseApi.exportMaterialWarehouse(queryParams)
    download.excel(data, '物料仓库.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 导入成功后的处理 */
const handleImportSuccess = () => {
  importDialogVisible.value = false
  getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
